--[[
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
--]]

----------------------------------------------------------------------
-- File: Hello.lua
-- Author: Mikael Kindborg
--
-- This programs shows how to use the MoSync API from Lua.
-- The program displays an image and you can move the image 
-- if you have a touch sensitive display.
-- Pressing the back key or zero key exits the program.
--
-- Note that this program makes direct use of the MoSync Syscall API
-- and explicitly handles the event loop. There is also a high-level 
-- Lua API that makes Lua programs shorter and more readable. That API
-- is currently being developed, and is used in the HelloLua example.
--
-- The function prefixed with "Sys" are helper functions for using the
-- MoSync API from Lua. A list of these functions are found in file:
-- MobileLuaLib/toluabindings/lua_helperapi.h
----------------------------------------------------------------------

function Main()

	-- Initialize environment for system events.
	SysEnvironmentInitialize()
	
	-- Create the MoSync event object.
	local event = SysEventCreate()
	
	-- Variable for the image to display.
	local image = nil
	
	-- Initiate download of the image.
	System.downloadImage(
		"http://www.mosync.com/sites/all/themes/custom/mosync2/images/phones.png",
		-- This code will get called when the image has downloaded.
		function (imageHandle)
			if nil ~= imageHandle then
				image = maCreatePlaceholder()
				--SysImageScale(imageHandle, nil, image, 300, 500, SCALETYPE_BILINEAR) 
				--SysImageScale(imageHandle, nil, image, 300, 500, SCALETYPE_NEAREST_NEIGHBOUR)
				SysImageScaleProportionally(imageHandle, nil, image, 2.0, SCALETYPE_BILINEAR) 
				maDestroyObject(imageHandle)
				DrawImage(image, 200, 200)
			end
		end)
	
	-- This is the event loop.
	while true do
		maWait(0)
		maGetEvent(event)
		local eventType = SysEventGetType(event)
		if EVENT_TYPE_CLOSE == eventType then
			-- Exit while loop.
			break
		elseif EVENT_TYPE_KEY_PRESSED == eventType then
			local key = SysEventGetKey(event)
			if MAK_BACK == key or MAK_0 == key then
				-- Exit while loop.
				break
			end
		elseif EVENT_TYPE_POINTER_PRESSED == eventType or
			   EVENT_TYPE_POINTER_DRAGGED == eventType then
			DrawImage(image, SysEventGetX(event), SysEventGetY(event))
		else
			SysEnvironmentHandleEvent(event)
		end
	end
	
	if nil ~= image then 
		maDestroyObject(image) 
	end
	
	maEventDelete(event)
end

function DrawImage(image, x, y)

	-- First draw the background.
	local screenWidth = EXTENT_X(maGetScrSize())
	local screenHeight = EXTENT_Y(maGetScrSize())
	maSetColor(0x2288FF)
	maFillRect(0, 0, screenWidth, screenHeight)
	
	-- Then draw the image, if it has downloaded.
	if nil ~= image then
		local imageHalfWidth = EXTENT_X(maGetImageSize(image)) / 2
		local imageHalfHeight = EXTENT_Y(maGetImageSize(image)) / 2
		maDrawImage(image, x - imageHalfWidth, y - imageHalfHeight)
	end
	
	-- Finally update the screen.
	maUpdateScreen()
end

-- Start the program by calling the Main function.
-- This must be done as the last thing, after defining
-- the functions used by the program.
Main()